﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL.Entity;

namespace DAL.Persistencia
{
    public class MoedaDAL
    {
        private NewTierraEntities Con;

        public MoedaDAL()
        {
            Con = new NewTierraEntities();
        }

        public Moeda ObterPorId(int MoedaId)
        {
            try
            {
                return Con.Moeda.Where(m => m.Moeda_id == MoedaId).SingleOrDefault();   
            }
            catch 
            {
                throw;
            }

        }

        public void Salvar(Moeda b)
        {
            try
            {
                Con.AddToMoeda(b);
                Con.SaveChanges();
            }
            catch
            {
                throw;
            }
        }

        public void Excluir(Moeda b)
        {
            try
            {
                Con.DeleteObject(b);
                Con.SaveChanges();
            }
            catch
            {
                throw;
            }
        }

        public void Atualizar(Moeda novo)
        {
            try
            {
                Moeda antigo = ObterPorId(novo.Moeda_id);

                antigo.Moeda_sigla = novo.Moeda_sigla;
                antigo.Moeda_nome = novo.Moeda_nome;

                Con.SaveChanges();
            }
            catch
            {
                throw;
            }
        }

        public List<Moeda> ListarTodos()
        {
            try
            {
                return Con.Moeda.OrderBy(c => c.Moeda_nome).ToList();
            }
            catch
            {
                throw;
            }
        }

        public bool ObterPorNome(string nome)
        {
            try
            {
                return Con.Moeda.Where(s => s.Moeda_nome.Equals(nome)).Count() != 0;
            }
            catch
            {
                throw;
            }
        }

        public Moeda RetornaIdPorTransfer(string Transf)
        {
            try
            {

                return (from ssv in Con.S_Servicos
                        join svc in Con.Servico_Completo on ssv.Servicos_Id equals svc.Servicos_Id
                        join mon in Con.Monta_Servico on svc.IdMServico equals mon.IdMServico
                        join sup in Con.Suppliers on mon.S_id equals sup.S_id
                        join sba in Con.S_Bases on sup.S_id equals sba.S_id
                        join mod in Con.Moeda on sba.Moeda_id equals mod.Moeda_id
                        where ssv.Servicos_Nome.Equals(Transf)
                        select mod).Distinct().FirstOrDefault();
            }
            catch 
            {                
                throw;
            }
        }

        public Moeda RetornaIdPorTransfer1(string Transf)
        {
            try
            {

                return (from ssv in Con.S_Servicos
                        join svc in Con.Servico_Completo on ssv.Servicos_Id equals svc.Servicos_Id
                        join mon in Con.Monta_Servico on svc.IdMServico_Temporada equals mon.IdMServico
                        join sup in Con.Suppliers on mon.S_id equals sup.S_id
                        join sba in Con.S_Bases on sup.S_id equals sba.S_id
                        join mod in Con.Moeda on sba.Moeda_id equals mod.Moeda_id
                        where ssv.Servicos_Nome.Equals(Transf) &&
                              sba.IdTipoTrans == 6
                        select mod).Distinct().SingleOrDefault();
            }
            catch
            {
                throw;
            }
        }

    }
}
